/*
 * @Author: your name
 * @Date: 2021-06-05 10:54:52
 * @LastEditTime: 2021-06-10 00:01:10
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \HW5\src\q5.cpp
 */
#include "../inc/q5.hpp"

Stack::Stack(/* args */)
{
    data=new int[20];
    MAXSize=20;
    num=0;
}

Stack::Stack(int size)
{   
    data=new int[size];
    MAXSize=size;
    num=0;
}

Stack::~Stack()
{  
    delete[] data;
}
void Stack::push_back(int i )
{
    if(num+1>MAXSize)
    {
        throw  std::overflow_error("超出堆栈范围");
    }
     *(data+num)=i;
    num++;
   
}
int Stack::pop()
{
    if(num-1<0)
    {
        throw std::underflow_error("堆栈为空");
    }
    int temp=*(data+num);
    *(data+num)=0;
    num--;
    return temp;
}
void testQ5()
{
    Stack s1(10);
    for (int i =0;i<11;++i)
    try
    {
       s1.push_back(i);
    }
    catch(const std::exception& e)
    {
        std::cerr << e.what() << '\n';
    }
      for (int i =0;i<11;++i)
    try
    {
       s1.pop();
    }
    catch(const std::exception& e)
    {
        std::cerr << e.what() << '\n';
    }
}